package cn.iocoder.yudao.module.system.controller.admin.gugu;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.subjectselection.MajorCategoryListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.subjectselection.SchoolListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.subjectselection.SubjectSelectionReqVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.vo.subjectselection.SubjectSelectionRespVO;
import cn.iocoder.yudao.module.system.service.gugu.SubjectSelectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;

/**
 * 智能选科控制器
 */
@Tag(name = "管理后台 - 智能选科")
@RestController
@RequestMapping("/system/subject-selection")
@Validated
@Slf4j
public class SubjectSelectionController {

    @Resource
    private SubjectSelectionService subjectSelectionService;

    @GetMapping("/major-categories")
    @Operation(summary = "获取专业大类列表", 
               description = "获取可选择的专业大类列表，包含每个大类的基本统计信息")
    @PermitAll
    public CommonResult<List<SubjectSelectionRespVO.MajorCategoryInfo>> getMajorCategoryList(
            @Parameter(description = "省份名称", example = "安徽") @RequestParam(value = "province", required = false) String province,
            @Parameter(description = "年份", example = "2027") @RequestParam(value = "year", required = false) Integer year,
            @Parameter(description = "教育级别", example = "本科") @RequestParam(value = "educationLevel", required = false) String educationLevel,
            @Parameter(description = "文理综合类别", example = "理科") @RequestParam(value = "type", required = false) String type) {
        
        MajorCategoryListReqVO reqVO = new MajorCategoryListReqVO();
        reqVO.setProvince(province);
        reqVO.setYear(year>2025?2025:year);
        reqVO.setEducationLevel(educationLevel);
        reqVO.setType(type);
        
        List<SubjectSelectionRespVO.MajorCategoryInfo> result = subjectSelectionService.getMajorCategoryList(reqVO);
        return CommonResult.success(result);
    }

    @GetMapping("/schools")
    @Operation(summary = "获取学校列表",
               description = "获取可选择的学校列表，包含每个学校的基本统计信息")
    @PermitAll
    public CommonResult<List<SubjectSelectionRespVO.SchoolInfo>> getSchoolList(
            @Parameter(description = "省份名称", example = "安徽") @RequestParam(value = "province", required = false) String province,
            @Parameter(description = "年份", example = "2027") @RequestParam(value = "year", required = false) Integer year,
            @Parameter(description = "教育级别", example = "本科") @RequestParam(value = "educationLevel", required = false) String educationLevel,
            @Parameter(description = "文理综合类别", example = "理科") @RequestParam(value = "type", required = false) String type,
            @Parameter(description = "学校名称关键字", example = "北京") @RequestParam(value = "schoolNameKeyword", required = false) String schoolNameKeyword,
            @Parameter(description = "专业大类筛选", example = "工学") @RequestParam(value = "majorCategory", required = false) String majorCategory) {

        SchoolListReqVO reqVO = new SchoolListReqVO();
        reqVO.setProvince(province);
        reqVO.setYear(year);
        reqVO.setEducationLevel(educationLevel);
        reqVO.setType(type);
        reqVO.setSchoolNameKeyword(schoolNameKeyword);
        reqVO.setMajorCategory(majorCategory);

        List<SubjectSelectionRespVO.SchoolInfo> result = subjectSelectionService.getSchoolList(reqVO);
        return CommonResult.success(result);
    }

    @PostMapping("/calculate")
    @Operation(summary = "计算最优选科组合", 
               description = "根据用户选择的专业大类，计算最优的选科组合并返回详细的匹配信息")
    @PermitAll
    public CommonResult<SubjectSelectionRespVO> calculateOptimalSubjectCombination(
            @RequestBody @Valid SubjectSelectionReqVO reqVO) {
        
        log.info("收到选科计算请求：{}", reqVO);
        
        try {
            SubjectSelectionRespVO result = subjectSelectionService.calculateOptimalSubjectCombination(reqVO);
            return CommonResult.success(result);
        } catch (Exception e) {
            log.error("计算最优选科组合失败", e);
            return CommonResult.error(500, "计算失败：" + e.getMessage());
        }
    }

    @GetMapping("/category-detail")
    @Operation(summary = "获取专业大类详细信息", 
               description = "获取指定专业大类的详细信息，包含选科要求统计和匹配率")
    @PermitAll
    public CommonResult<SubjectSelectionRespVO.MajorCategoryInfo> getMajorCategoryDetail(
            @Parameter(description = "专业大类名称", required = true, example = "哲学类") @RequestParam("categoryName") String categoryName,
            @Parameter(description = "省份名称", example = "安徽") @RequestParam(value = "province", required = false) String province,
            @Parameter(description = "年份", example = "2027") @RequestParam(value = "year", required = false) Integer year,
            @Parameter(description = "教育级别", example = "本科") @RequestParam(value = "educationLevel", required = false) String educationLevel,
            @Parameter(description = "文理综合类别", example = "理科") @RequestParam(value = "type", required = false) String type) {
        
        try {
            SubjectSelectionRespVO.MajorCategoryInfo result = subjectSelectionService.getMajorCategoryInfo(
                    categoryName, province, year, educationLevel, type);
            
            if (result == null) {
                return CommonResult.error(404, "未找到指定的专业大类信息");
            }
            
            return CommonResult.success(result);
        } catch (Exception e) {
            log.error("获取专业大类详细信息失败", e);
            return CommonResult.error(500, "查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/school-detail")
    @Operation(summary = "获取学校详细信息",
               description = "获取指定学校的详细信息，包含选科要求统计和匹配率")
    @PermitAll
    public CommonResult<SubjectSelectionRespVO.SchoolInfo> getSchoolDetail(
            @Parameter(description = "学校名称", required = true, example = "北京大学") @RequestParam("schoolName") String schoolName,
            @Parameter(description = "省份名称", example = "安徽") @RequestParam(value = "province", required = false) String province,
            @Parameter(description = "年份", example = "2027") @RequestParam(value = "year", required = false) Integer year,
            @Parameter(description = "教育级别", example = "本科") @RequestParam(value = "educationLevel", required = false) String educationLevel,
            @Parameter(description = "文理综合类别", example = "理科") @RequestParam(value = "type", required = false) String type) {

        try {
            SubjectSelectionRespVO.SchoolInfo result = subjectSelectionService.getSchoolInfo(
                    schoolName, province, year, educationLevel, type);

            if (result == null) {
                return CommonResult.error(404, "未找到指定的学校信息");
            }

            return CommonResult.success(result);
        } catch (Exception e) {
            log.error("获取学校详细信息失败", e);
            return CommonResult.error(500, "查询失败：" + e.getMessage());
        }
    }
}
